package com.hellogood.yaohuo.server.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.db.DBConnection;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.Detail;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.entity.YhForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class YhFormDAO extends FormDAO {

	Logger log = Logger.getLogger(this.getClass());

	public String createYaohuo(YhForm form, boolean isModify) {
		String result = "";
		List<String> sqls = new ArrayList<String>();
		if (isModify) {
			String sql = "DELETE FROM yhForm WHERE YhBillNo = '" + form.getBillNo() + "'";
			sqls.add(sql);
			sql = "DELETE FROM yhDetail WHERE YhBillNo = '" + form.getBillNo() + "'";
			sqls.add(sql);
		}

		String sql = "INSERT INTO YhForm(yhBillNo,yhDate,orgCode,counts,cost,netCost,total,staffCode,staffName,rzDate,remark,createTime,Qtyh) VALUES('" + form.getBillNo()
				+ "',CONVERT(varchar(100), GETDATE(), 23),'" + form.getOrgCode() + "'," + form.getCounts() + "," + form.getCost() + "," + form.getNetCost() + "," + form.getTotal() + ",'"
				+ form.getStaffCode() + "','" + form.getStaffName() + "','" + form.getRzDate() + "','" + form.getRemark() + "',GETDATE(),'" + form.getZiyou() + "')";
		sqls.add(sql);

		List<Detail> list = form.getList();
		if (list != null && (!list.isEmpty())) {
			for (Detail d : list) {
				sql = "INSERT INTO YhDetail(yhBillNo,pluCode,pluName,barCode,depCode,depName,bra,color,size_s,size_m,size_l,size_xl,size_xxl,size_xxxl,size_avg,jPrice,netJPrice,price,counts,cost,"
						+ "netCost,total,rzDate,remark,orgCode,createTime) VALUES('"
						+ d.getBillNo()
						+ "','"
						+ d.getPluCode()
						+ "','"
						+ d.getPluName()
						+ "','"
						+ d.getBarCode()
						+ "','"
						+ d.getDepCode()
						+ "','"
						+ d.getDepName()
						+ "','"
						+ d.getBra()
						+ "','"
						+ d.getColor()
						+ "',"
						+ d.getSize_s()
						+ ","
						+ d.getSize_m()
						+ ","
						+ d.getSize_l()
						+ ","
						+ d.getSize_xl()
						+ ","
						+ d.getSize_xxl()
						+ ","
						+ d.getSize_xxxl()
						+ ","
						+ d.getSize_avg()
						+ ","
						+ d.getjPrice()
						+ ","
						+ d.getjPrice()
						+ ","
						+ d.getPrice()
						+ ","
						+ d.getCounts()
						+ ","
						+ d.getCost()
						+ ","
						+ d.getCost()
						+ ","
						+ d.getTotal()
						+ ",'"
						+ d.getRzDate() + "','" + d.getRemark() + "','" + d.getOrgCode() + "',getDate())";
				sqls.add(sql);
			}
		}
		try {
			executeBatch(sqls);
		} catch (DatabaseException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return e.getMessage();
		}
		return result;
	}

	public String rz(String bill) {
		String result = "";
		String sql = "UPDATE yhForm SET rzDate = CONVERT(VARCHAR(10),getDate(),120) WHERE yhBillNo = '" + bill + "'";
		try {
			super.execute(sql);
		} catch (DatabaseException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return e.getMessage();
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<Form> searchForm(String tableFormName, String billNoField, String dateField, String orgCode, String startDate, String endDate, String rz) {
		String hxDbName = DBConnection.hxDbName;
		String sql = "SELECT (SELECT OrgName FROM " + hxDbName + "..SubShop WHERE OrgCode = f.OrgCode) AS OrgName,f.*,f." + billNoField + " AS billNo,f." + dateField + " AS billDate FROM "
				+ tableFormName + " f WHERE f.OrgCode = '" + orgCode + "' AND cast(f." + dateField + " as char(10)) >= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + startDate + "'),120) and cast(f."
				+ dateField + " as char(10)) <= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + endDate + "'),120)";
		if (rz.equals(Constants.RUZHANG_CODE_YES)) {
			sql = sql + " AND rzDate <> ''";
		} else if (rz.equals(Constants.RUZHANG_CODE_NO)) {
			sql = sql + " AND rzDate = ''";
		}
		List<?> list = super.query(sql);
		return (List<Form>) list;
	}

	@SuppressWarnings("unchecked")
	public List<Form> searchFormByQuyu(String tableFormName, String billNoField, String dateField, String quyuCode, String startDate, String endDate, String rz) {
		String hxDbName = DBConnection.hxDbName;
		String userCode = MainFrame.currentUser.getStaffCode();
		String orgs = "";
		if (quyuCode == null) {
			orgs = MgrFactory.getInstance().createOrgMgr().getOrgCodeByUser(userCode);
		} else {
			orgs = MgrFactory.getInstance().createOrgMgr().getOrgCodeByUserQuyu(userCode, quyuCode);
		}

		String sql = "SELECT (SELECT OrgName FROM " + hxDbName + "..SubShop WHERE OrgCode = f.OrgCode) AS OrgName,f.*,f." + billNoField + " AS billNo,f." + dateField + " AS billDate FROM "
				+ tableFormName + " f WHERE f.OrgCode IN (" + orgs + ") AND cast(f." + dateField + " as char(10)) >= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + startDate + "'),120) and cast(f."
				+ dateField + " as char(10)) <= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + endDate + "'),120)";
		if (rz.equals(Constants.RUZHANG_CODE_YES)) {
			sql = sql + " AND rzDate <> ''";
		} else if (rz.equals(Constants.RUZHANG_CODE_NO)) {
			sql = sql + " AND rzDate = ''";
		}
		List<?> list = super.query(sql);
		return (List<Form>) list;
	}
}
